package com.xwintop.xTransfer.filter.util;

import com.xwintop.xJavaFxTool.utils.SpringUtil;
import com.xwintop.xTransfer.filter.bean.FilterConfig;
import com.xwintop.xTransfer.messaging.DefaultMessage;
import com.xwintop.xTransfer.messaging.IMessage;
import com.xwintop.xTransfer.util.Common;
import com.xwintop.xcore.commons.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @ClassName: FilterServiceUtil
 * @Description: Filter服务工具类
 * @author: xufeng
 * @date: 2021/3/30 14:28
 */

@Slf4j
public class FilterServiceUtil {
    //检查消息是否继续执行filter
    public static boolean checkFilterMessageIsResume(FilterConfig filterConfig, IMessage iMessage, Map params) {
        if (StringUtils.isNotBlank(filterConfig.getFileNameFilterRegex())) {
            if (!iMessage.getFileName().matches(filterConfig.getFileNameFilterRegex())) {
                log.info("Filter:" + filterConfig.getId() + "跳过fileName：" + iMessage.getFileName());
                return false;
            }
        }
//        if (iMessage.getProperty(DefaultMessage.GATEWAY_RESUME_JOB_ID) != null) {
//            if (!iMessage.getProperty(DefaultMessage.GATEWAY_RESUME_JOB_ID).toString().contains(filterConfig.getId())) {
//                log.info("Filter提前跳过本次任务:" + filterConfig.getId() + " fileName：" + iMessage.getFileName());
//                return false;
//            }
//        }
//        if (StringUtils.isNotBlank(filterConfig.getFileNameFilterRegex())) {
//            CacheUtil cacheUtil = SpringUtil.getBean("cacheUtil", CacheUtil.class);
//            if ("?!".equals(filterConfig.getFileNameFilterRegex())) {
//                if (cacheUtil.get("gateWayFileNameFilterRegexGroup", iMessage.getId() + filterConfig.getFileNameFilterRegexGroup()) != null) {
//                    log.info("Filter ?!:" + filterConfig.getId() + " 跳过fileName:" + iMessage.getFileName());
//                    return false;
//                }
//            } else {
//                if (!iMessage.getFileName().matches(filterConfig.getFileNameFilterRegex())) {
//                    log.info("Filter:" + filterConfig.getId() + " 跳过fileName:" + iMessage.getFileName());
//                    return false;
//                }
//                cacheUtil.set("gateWayFileNameFilterRegexGroup", iMessage.getId() + filterConfig.getFileNameFilterRegexGroup(), 1);
//            }
//        }
//        if (Common.checkFilterVariableRegex(filterConfig.getFilterVariableRegexMap(), iMessage, params)) {
//            log.info("FilterVariable:" + filterConfig.getId() + " 跳过fileName:" + iMessage.getFileName());
//            return false;
//        }
        return true;
    }
}
